package me.youline.dataServer.service.support;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;

import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.ApnsServiceBuilder;

/**
 * 苹果推送服务业务类初始化对象，它可以根据spring active profile，选择连接苹果测试服务和苹果正式服务。
 * 1.当profile 为production 则连接正式服务
 * 2.当profile 非production 则连接测试服务
 * @author linhan
 *
 */
public class ApnsServiceFactoryBean implements FactoryBean<ApnsService>{

	@Value("#{frontProperties['apns.password']}")
	private String apnsPassword;
	
	@Value("#{frontProperties['apns.cert.path']}")
	private String certClassPath;
	
	@Autowired
	private Environment env;
	
	@Override
	public ApnsService getObject() throws Exception {
		ApnsServiceBuilder p = APNS.newService().withAppleDestination(env.acceptsProfiles("production"))
	    .withCert(new ClassPathResource(certClassPath).getInputStream(), apnsPassword)
	    .asPool(3);
		return p.build();
	}

	@Override
	public Class<?> getObjectType() {
		return ApnsService.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
